package com.inspectortime.mail.mailer;

import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;


/**
 * Just a typical Mailer implementation that allows for sending a 
 * MailMessage in html format 
 * 
 * @author bones
 *
 */
public class HtmlMailerImpl implements Mailer {

	protected static Log log = LogFactory.getLog(HtmlMailerImpl.class);
    
    private JavaMailSender mailSender;
    private int multiPartMode = MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED;
    private String encoding = "UTF-8";
    
    public HtmlMailerImpl(JavaMailSender mailSender) {
    	this.mailSender = mailSender;
    }
    
    
    /**
     * Uses JavaMailSender to send this message as a MimeMessage, offering support for 
     * HTML text content, inline elements, etc
     */
    public void sendMail(MailMessage mailMsg) {
    	
    	final SimpleMailMessage message = (SimpleMailMessage) mailMsg;
    	
	    	MimeMessagePreparator preparator = new MimeMessagePreparator() {
	            public void prepare(MimeMessage mimeMessage) throws Exception {
	                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, getMultiPartMode(), getEncoding());
	                helper.setFrom(message.getFrom());
	                helper.setTo(message.getTo());
	                helper.setSubject(message.getSubject());
	                helper.setText(message.getText(),true); //true means enable html email
	                
	                //Do this to add an attachment (http://static.springframework.org/spring/docs/2.0.x/reference/mail.html)
	    	    	//FileSystemResource file = new FileSystemResource(new File("c:/Sample.jpg"));
	    	    	//helper.addAttachment("CoolImage.jpg", file);

	            }
	        };
	    	
	    	this.mailSender.send(preparator);   
    	
    }
 
    

    /**
     * @return the desired multipart mode for the email to be sent. 
     *          Defaults to MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED;
     */
    public int getMultiPartMode() {
        return multiPartMode;
    }

    /**
     * Defaults to MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, so this should not need to be set. Added just in case...
     * @param multiPartMode
     */
    public void setMultiPartMode(int multiPartMode) {
        this.multiPartMode = multiPartMode;
    }


    /**
     * @return the desired encoding type. Defaults to UTF-8
     */
    public String getEncoding() {
        return encoding;
    }


    /**
     * Defaults to UTF-8, so this should not need to be set. Added just in case...
     * @param encoding
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }



    
}
